package com.website.web.back.controller.website;

import com.website.common.annotation.Log;
import com.website.common.core.controller.BaseController;
import com.website.common.core.domain.AjaxResult;
import com.website.common.core.page.TableDataInfo;
import com.website.common.enums.BusinessType;
import com.website.common.utils.StringUtils;
import com.website.common.utils.poi.ExcelUtil;
import com.website.custom.domain.WServices;
import com.website.custom.service.IWServicesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务项目Controller
 * 
 * @author ruoyi
 * @date 2024-12-03
 */
@Controller
@RequestMapping("/back/custom/services")
public class WServicesController extends BaseController
{
    private String prefix = "back/website/services";

    @Autowired
    private IWServicesService wServicesService;

    @RequiresPermissions("system:services:view")
    @GetMapping()
    public String services()
    {
        return prefix + "/services";
    }

    /**
     * 查询服务项目列表
     */
    @RequiresPermissions("system:services:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WServices wServices)
    {
        startPage();
        List<WServices> list = wServicesService.selectWServicesList(wServices);
        return getDataTable(list);
    }

    /**
     * 导出服务项目列表
     */
    @RequiresPermissions("system:services:export")
    @Log(title = "服务项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WServices wServices)
    {
        List<WServices> list = wServicesService.selectWServicesList(wServices);
        ExcelUtil<WServices> util = new ExcelUtil<WServices>(WServices.class);
        return util.exportExcel(list, "服务项目数据");
    }

    /**
     * 新增服务项目
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存服务项目
     */
    @RequiresPermissions("system:services:add")
    @Log(title = "服务项目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WServices wServices)
    {
        Long maxId = wServicesService.getMaxId();
        wServices.setPrewId(maxId);
        wServicesService.insertWServices(wServices);
        // 更新上一条
        WServices prewServices = wServicesService.selectWServicesById(maxId);
        prewServices.setNextId(wServices.getId());
        wServicesService.updateWServices(prewServices);
        return toAjax(1);
    }

    /**
     * 修改服务项目
     */
    @RequiresPermissions("system:services:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WServices wServices = wServicesService.selectWServicesById(id);
        mmap.put("wServices", wServices);
        return prefix + "/edit";
    }

    /**
     * 修改保存服务项目
     */
    @RequiresPermissions("system:services:edit")
    @Log(title = "服务项目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WServices wServices)
    {
        if (StringUtils.isEmpty(wServices.getImg())) {
            WServices wServices1 = wServicesService.selectWServicesById(wServices.getId());
            String img = wServices1.getImg();
            if (!StringUtils.isEmpty(img)) {
                wServices.setImg(img);
            }
        }
        return toAjax(wServicesService.updateWServices(wServices));
    }

    /**
     * 删除服务项目
     */
    @RequiresPermissions("system:services:remove")
    @Log(title = "服务项目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wServicesService.deleteWServicesByIds(ids));
    }
}
